/**
 * 
 */
package org.hotpotmaterial.codegenn2.controller.v1;

import java.util.List;

import org.hotpotmaterial.codegenn2.common.ResultCodeConstants;
import org.hotpotmaterial.codegenn2.dto.ResultBaseDTO;
import org.hotpotmaterial.codegenn2.dto.ResultDataDTO;
import org.hotpotmaterial.codegenn2.dto.ResultPageDTO;
import org.hotpotmaterial.codegenn2.dto.entity.PageDTO;
import org.hotpotmaterial.codegenn2.dto.entity.v1.HmteamAccountDTO;
import org.hotpotmaterial.codegenn2.dto.request.PageRequestDTO;
import org.hotpotmaterial.codegenn2.dto.request.PasswordRequestDTO;
import org.hotpotmaterial.codegenn2.entity.HmteamAccount;
import org.hotpotmaterial.codegenn2.service.IHmteamAccountService;
import org.hotpotmaterial.codegenn2.utils.RequestUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.common.collect.Lists;

import io.swagger.annotations.Api;

/**
 * @author Wenxing
 *
 */
@Controller
@RequestMapping("/hmteam/codegen/v1/accounts")
@Api(value = "账号Controller", tags={"账号管理"})
public class HmteamAccountController {

	@Autowired
	private IHmteamAccountService hmteamAccountService;

	@PostMapping("/register")
	public ResponseEntity<ResultBaseDTO> postRegister(@RequestBody HmteamAccount record) {
		// 业务
		HmteamAccount serviceResult = hmteamAccountService.insertAccount(record);
		// 转换
		HmteamAccountDTO data = new HmteamAccountDTO();
		BeanUtils.copyProperties(serviceResult, data);
		return new ResponseEntity<>(ResultDataDTO.<HmteamAccountDTO>builder().data(data)
				.msg(ResultCodeConstants.MSG_SUCCESS).code(ResultCodeConstants.CODE_SUCCESS).build(), HttpStatus.OK);
	}

	@PutMapping("/login_user/upt_pwd")
	public ResponseEntity<ResultBaseDTO> putUptPassword(@RequestBody PasswordRequestDTO password) {
		// 获取当前登录账户id
		HmteamAccount loginUser = RequestUtil.getAccountInfo();
		hmteamAccountService.updatePassword(loginUser.getId(), password);
		// 返回对象
		ResultBaseDTO result = new ResultBaseDTO();
		result.setMsg(ResultCodeConstants.MSG_SUCCESS);
		result.setCode(ResultCodeConstants.CODE_SUCCESS);
		return new ResponseEntity<>(result, HttpStatus.OK);
	}

	@PostMapping("/pages")
	public ResponseEntity<ResultBaseDTO> postPages(@RequestBody PageRequestDTO searchDTO) {
		// 获取当前登录账户id
		PageDTO<HmteamAccount> serviceResult = hmteamAccountService.findAccountsByUsername(searchDTO);
		// 复制属性
		List<HmteamAccountDTO> dataList = Lists.newArrayList();
		serviceResult.getData().forEach((item) -> {
			HmteamAccountDTO data = new HmteamAccountDTO();
			BeanUtils.copyProperties(item, data, "password");
			dataList.add(data);
		});
		return new ResponseEntity<>(
				ResultPageDTO.<HmteamAccountDTO>builder().pageNum(serviceResult.getPageNum())
						.pageSize(serviceResult.getPageSize()).total(serviceResult.getTotal()).data(dataList)
						.code(ResultCodeConstants.CODE_SUCCESS).msg(ResultCodeConstants.MSG_SUCCESS).build(),
				HttpStatus.OK);
	}

}
